{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dipy.data import get_data\n",
    "import nibabel as nib\n",
    "from dipy.io import read_bvals_bvecs\n",
    "from dipy.core.gradients import gradient_table\n",
    "from dipy.reconst.dti import TensorModel\n",
    "from dipy.reconst.dti import fractional_anisotropy\n",
    "from dipy.reconst.dti import color_fa\n",
    "\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.image as mpimg\n",
    "import numpy as np\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def makeColor_fa(fimg, fbval, fbvec):\n",
    "    img = nib.load(fimg)\n",
    "    data = img.get_data()\n",
    "\n",
    "    bvals, bvecs = read_bvals_bvecs(fbval, fbvec)\n",
    "\n",
    "    gtab = gradient_table(bvals, bvecs)\n",
    "\n",
    "    ten = TensorModel(gtab)\n",
    "    tenfit = ten.fit(data)\n",
    "\n",
    "    fa = fractional_anisotropy(tenfit.evals)\n",
    "    cfa = color_fa(fa, tenfit.evecs)\n",
    "    return cfa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "fimg='DTI_004.nii.gz'\n",
    "fbvec='DTI_004.bvec'\n",
    "fbval='DTI_004.bval'\n",
    "niixFA = makeColor_fa(fimg, fbval, fbvec)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fimg='DWIDTI_004.nii'\n",
    "fbvec='DWIDTI_004.bvec'\n",
    "fbval='DWIDTI_004.bval'\n",
    "dcmconvertFA = makeColor_fa(fimg, fbval, fbvec)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "imgplot = plt.imshow(niixFA[:,64,:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "imgplot = plt.imshow(dcmconvertFA[:,64,:])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
